limsProductMain.pjChoosePage = {

    getGridParam: function () {
        var _this = this, columns = limsProductMain.getPJChooseListColumns();
        _this.param.single ? columns.unshift({
            radio: true
        }) : columns.unshift({
            checkbox: true
        });
        return {
            type: 'grid',
            id: 'lims-pj-product-main-choose-list-grid',
            url: limsProductMain.baseUrl + '/queries/choosable',
            requestData: _this.param,
            columns: columns,
            filterOpen: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    _this.add()
                }
            }],
            onBeforeLoad: function (data) {
                var choosenData
                Gikam.getAsyncComp('lims-pj-product-main-choose-list-grid-choosen').done(function (grid) {
                    choosenData = grid.getData()
                })
                if (!Gikam.isEmptyObject(choosenData)) {
                    data = data.filter(function (item) {
                        var flag = true
                        Gikam.each(choosenData, function (i, row) {
                            if (row.id === item.id) {
                                flag = false
                            }
                        })
                        return flag
                    })
                }
                return data
            }
        }
    },

    getChoosenGridParam: function () {
        var _this = this, columns = limsProductMain.getPJChooseListColumns()
        _this.param.single ? columns.unshift({
            radio: true
        }) : columns.unshift({
            checkbox: true
        })
        let queryUrl = limsProductMain.baseUrl + '/queries'
        let rows = []
        Gikam.postSync(queryUrl, Gikam.getJsonWrapper({
            f: {
                choosenInPackage: _this.param.packageid
            },
            s: 100
        })).done(req => {
            rows.push(...req.rows)
        })
        return {
            type: 'grid',
            id: 'lims-pj-product-main-choose-list-grid-choosen',
            columns: columns,
            filterOpen: true,
            data: rows,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove',
                onClick: function () {
                    _this.remove()
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.CONFIRM',
                icon: 'select',
                onClick: function () {
                    Gikam.getLastModal().close(Gikam.getComp('lims-pj-product-main-choose-list-grid-choosen').getData())
                }
            }]
        }
    },

    add: function () {
        var _this = this
        var chooseMultListGrid = Gikam.getComp('lims-pj-product-main-choose-list-grid')
        var choosenMultListGrid = Gikam.getComp('lims-pj-product-main-choose-list-grid-choosen')
        var addUserData = []
        var choosenData = choosenMultListGrid.getData() ? choosenMultListGrid.getData() : [],
            removedRowIndex = chooseMultListGrid.model.$children[0].checkeds

        var chooseData = Gikam.jQuery.map(chooseMultListGrid.getData(), function (rowData, index) {
            if (Gikam.jQuery.inArray(index, removedRowIndex) < 0) {
                return rowData
            }
            addUserData.push(rowData)
        })
        choosenData = choosenData.concat(addUserData)
        _this.arrangeGrid(choosenData)

    },

    remove: function () {
        var _this = this, chooseMultListGrid = Gikam.getComp('lims-pj-product-main-choose-list-grid'),
            choosenMultListGrid = Gikam.getComp('lims-pj-product-main-choose-list-grid-choosen'),
            oldChoosenData = choosenMultListGrid.getData(), delChoosenData = choosenMultListGrid.getSelections(),
            removedRowIndex = choosenMultListGrid.model.$children[0].checkeds

        var choosenData = Gikam.jQuery.map(oldChoosenData, function (rowData, index) {
            if (Gikam.jQuery.inArray(index, removedRowIndex) < 0) {
                return rowData
            }
        })

        var delMessage = Gikam.propI18N('GIKAM.TIP.CONFIRM_TO_DELETE_FOLLOWING_ITEMS') + ':<br/>'
        delChoosenData.forEach(function (row) {
            delMessage = delMessage + row.productCode + '-' + row.productName + '<br/>'
        })
        Gikam.confirm('MODULE.BUSINESS.BATCH.ADD.WAINING', delMessage, function () {
            _this.arrangeGrid(choosenData, delChoosenData)
        })
    },

    arrangeGrid: function (choosenData) {
        var chooseMultListGrid = Gikam.getComp('lims-pj-product-main-choose-list-grid'),
            choosenMultListGrid = Gikam.getComp('lims-pj-product-main-choose-list-grid-choosen')

        choosenMultListGrid.refreshByData({
            rows: choosenData
        })
        chooseMultListGrid.refresh()
        choosenMultListGrid.selectedRowIndex = []

    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-purpose-choose-list-layout',
            renderTo: Gikam.getLastModal().window.$dom,
            west: {
                width: '50%',
                items: [this.getGridParam()]
            },
            center: {
                items: [this.getChoosenGridParam()]
            }
        })
    },

    init: function (param) {
        this.param = param;
        this.create();
    }
};
